package com.zvfe.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zvfe.common.QueryPageParam;
import com.zvfe.common.R;
import com.zvfe.pojo.User;
import com.zvfe.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wny
 * @since 2023-06-28
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //检查用户名是否重复
    @GetMapping("/findByUsername")
    public R findByUsername(@RequestParam String username) {
        List<User> list = userService.lambdaQuery().eq(User::getUsername, username).list();
        return list.size() > 0 ? R.success(list) : R.err();
    }

    //新增
    @PostMapping("/add")
    public R add(@RequestBody User user) {
        return userService.save(user) ? R.success() : R.err();
    }

    //根据Id修改修改
    @PutMapping("/update")
    public R updateById(@RequestBody User user) {
        return userService.updateById(user) ? R.success() : R.err();
    }

    //根据ID删除
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable Integer id) {
        return userService.removeById(id) ? R.success() : R.err();
    }


    //根据id查询
    @GetMapping("/{id}")
    public User selectById(@PathVariable Integer id) {
        return userService.getById(id);
    }

    //根据名字模糊查询
    @PostMapping("/listP")
    public R selectByName(@RequestBody User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        String name = user.getName();
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like(name != null, User::getName, name);
        }
        List<User> list = userService.list(queryWrapper);
        int size = list.size();
        Long total = Long.valueOf(size);
        return R.success(list, total);
    }

    //管理员分页查询
    @PostMapping("/listPage")
    public R listPage(@RequestBody QueryPageParam param) {
        HashMap hm = param.getParam();
        Page<User> page = new Page<>();
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        String roleId = (String) hm.get("roleId");
        String name = (String) hm.get("name");
        String sex = (String) hm.get("sex");
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), User::getName, name)
                .eq(StringUtils.isNotEmpty(sex), User::getSex, sex)
                .eq(StringUtils.isNotEmpty(roleId),User::getRoleId,roleId)
                .orderByAsc(User::getId);
        IPage<User> result = userService.page(page, wrapper);
        return R.success(result.getRecords(), result.getTotal());
    }

}
